package it.polimi.sartori_daniello_picca.meteocal.gui;

import it.polimi.sartori_daniello_picca.meteocal.boundary.EventManager;
import it.polimi.sartori_daniello_picca.meteocal.boundary.NotificationManager;
import it.polimi.sartori_daniello_picca.meteocal.control.CheckModifications;
import it.polimi.sartori_daniello_picca.meteocal.control.PasswordEncrypter;
import it.polimi.sartori_daniello_picca.meteocal.entity.Calendar;
import it.polimi.sartori_daniello_picca.meteocal.entity.Event;
import it.polimi.sartori_daniello_picca.meteocal.entity.Users;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Ale
 */
@ManagedBean
@ViewScoped
public class EventBean implements Serializable {
    
    @EJB
    private EventManager em;
    
    @EJB
    private NotificationManager nm;
    
    @EJB
    private CheckModifications cm;
    
    private Event event;
    private List<Event> eventbyuser;
    private String avviso;
    private String messaggio;
   
    public EventBean() {
    }
    
    public String getAvviso() {
        return this.avviso;
    }
    
    public void setAvviso(String avviso) {
        this.avviso = avviso;
    }
    
     public String getMessaggio() {
        return this.messaggio;
    }
    
    public void setMessaggio(String messaggio) {
        this.messaggio= messaggio;
    }
    
    public Event getEvent() {
        return event;
    }
    
     public void setEvent(Event event) {
        this.event = event;
     }
   
     public Date stringToDate(String string) throws ParseException {
        
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
    Date date = format.parse(string);
    return (date);
     }
     
    public String createevent(String username){
        em.create(event, username);
        return "/user/createdevent?faces-redirect=true&e="+event.getEventId().toString();
    }
   
    public void modifyevent() {
        //gestire il meteo
    if (!cm.checkDoubleEventName(event.getEventName())) {
            avviso = "Select another event name, this one already exists";
    } 
        else {    
    em.edit(event);
    List<Calendar> cal = em.findParticipants(event);
    nm.notifmodify(event, cal);
    this.messaggio = "Succesful update!";
    }
    }
    
    @PostConstruct
    public void init() {
        event = new Event();
    }
    
     public Date getToday(){
        return new Date();
    }

     
     
}
